//
//  XMSelectedPhotosView.m
//  XMWeibo
//
//  Created by chi on 14-8-29.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMSelectedPhotosView.h"


@interface XMSelectedPhotosView ()



@end

@implementation XMSelectedPhotosView


- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        // 设置属性
        self.backgroundColor = [UIColor clearColor];
        
        // 实例化数组
        self.images = [NSMutableArray array];
        
        // 添加imageview视图，暂时支持一张
        [self setupImageViews];
        
    }
    
    return self;
}



/**
 *  添加imageview视图
 */
- (void)setupImageViews
{
    for (int i = 0; i < 9; ++i) {
        UIImageView *imageView = [[UIImageView alloc]init];
        [self addSubview:imageView];
        imageView.backgroundColor = [UIColor clearColor];
    }
    
}


/**
 *  添加图片
 */
- (NSMutableArray*)addImage:(UIImage*)image
{
    if (self.images.count == 9) {
        self.images[self.images.count - 1] = image;
    }
    else {
        [self.images addObject:image];
    }
    
    for (int i = 0; i < self.images.count; ++i) {
        UIImageView *imageView = self.subviews[i];
        imageView.image = self.images[i];
    }
    

    return self.images;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSUInteger count = self.subviews.count;
    CGFloat imageW = self.bounds.size.width / 5;
    CGFloat imageH = self.bounds.size.height / 2;
    CGFloat imageX = 0.0;
    CGFloat imageY = 0.0;
    
    for (int i = 0; i < count; ++i) {
        UIImageView *imageView = self.subviews[i];
        imageY = i / 5 * imageH;
        imageX = i % 5 * imageW;
        
        imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
    }
}

@end
